package ch.kerbtier.malurus.components.defaultrenderers;

import java.io.PrintWriter;

import ch.kerbtier.malurus.components.SingleSelect;
import ch.kerbtier.malurus.renderers.JavaComponentRenderer;

public class SingleSelectRenderer extends JavaComponentRenderer<SingleSelect> {
	@Override
	public void render() {
		PrintWriter w = getWriter();
		w.print("<select id=\"" + getSubject().getClientId() + "\" name=\"" + getSubject().getClientId()
				+ "\" size=\"1\"");
		if(getSubject().isSubmitOnSelect()){
			w.print(" onchange=\"window.bui.load('" + getSubject().getClientId() + "')\"");
		}
		w.print(">");
		int cnt = 0;
		for (Object value : getSubject().getModel()) {
			w.print("<option value=\"" + cnt + "\"");

			if (value.equals(getSubject().getSelectionModel().getValue())) {
				w.print(" selected=\"selected\"");
			}
			w.print("\">" + value.toString() + "</option>");
			cnt++;
		}
		w.print("</select>");
	}
}
